<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;
use think\response\Json;

class RepresentMessage extends AuthBase
{

    public function save(){
        list($rid, $name,$mobile,$content) = Param::postMore([
            ['rid',null],
            ['name',''],
            ['mobile',''],
            ['content',''],
        ], $this->request, true);

        $data=[
            'rid'=>$rid,
            'name'=>$name,
            'mobile'=>$mobile,
            'content'=>$content
        ];
        if(empty($rid)){
            return Show::error([],'rid不能为空');
        }
        if(empty($name)){
            return Show::error([],'反映人姓名不能为空');
        }
        if(empty($content)){
            return Show::error([],'问题描述不能为空');
        }
        $data['uid']=$this->userId;
        try {
            $result = (new \app\common\business\RepresentMessage())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    public function index(): Json
    {
        $page=$this->request->get('page');
        $limit=$this->request->get('limit');

        $bis = new \app\common\business\RepresentMessage();

        $map = [];

        try {
            $list = $bis->getListByWhereWithOrder($map,$page,$limit);
            $count = $bis->getCountByWhere($map);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        $result = [
            'count'=>$count,
            'list'=>$list
        ];
        return Show::success($result);
    }

    public function detail(){
        $id=$this->request->get('id');
        if(empty($id)){
            return Show::error([],'参数错误');
        }
        try {
            $data=(new \app\common\business\RepresentMessage())->getNormalById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }
}
